package pt.iscte.pcd.nestedclasses;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Container;

// import java.awt.GridLayout;
import java.awt.FlowLayout;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class NestedClassButtons {
    private final String STATIC_LISTENER_NAME    = "Static class listener";
    private final String INNER_LISTENER_NAME     = "Inner class listener";
    private final String LOCAL_LISTENER_NAME     = "Local class listener";
    private final String ANONYMOUS_LISTENER_NAME = "Anonymous class listener";
    private final String[] LISTENER_TYPES = {STATIC_LISTENER_NAME, INNER_LISTENER_NAME, LOCAL_LISTENER_NAME, ANONYMOUS_LISTENER_NAME }; 
    private JComboBox combobox;
    private JFrame window;
    private JButton add_button;
		
	public NestedClassButtons () {
        window = new JFrame("Nested class buttons");
    	Container container = window.getContentPane();
        container.setLayout(new FlowLayout());
        
        combobox = new JComboBox(LISTENER_TYPES); 
        container.add(combobox);        
        
        add_button = new JButton("Add button");
        container.add(add_button);        
        add_button.addActionListener(new AddButtonListener()); 

        window.setSize(300, 400);
        window.setLocation(200, 100);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void execute() {
        window.setVisible(true);
    }
    
    private static class StaticListener implements ActionListener {
    	NestedClassButtons app;
    	
    	public StaticListener(NestedClassButtons app) {
    		this.app = app;
    	}
    	public void actionPerformed(ActionEvent e) {
    		JOptionPane.showMessageDialog(null, "Static listener called");
    		app.removeButton((JButton)e.getSource());
    	}    	
    }

    private class InnerListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		JOptionPane.showMessageDialog(window, "Inner listener called");
    		removeButton((JButton)e.getSource());
    	}    	
    }

    private class AddButtonListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		String         button_label     = "No label set";
    		ActionListener action_listener  = null;
    		Color          foreground_color = Color.RED;
    		Color          background_color = Color.BLACK;
    		
    		if (((String) combobox.getSelectedItem()).equals(STATIC_LISTENER_NAME))
    		{
    			button_label     = "I have a static class listener";
    			action_listener  = new StaticListener(NestedClassButtons.this);
    			foreground_color = Color.BLUE;    		    	
    		} 
    		else if (((String) combobox.getSelectedItem()).equals(INNER_LISTENER_NAME))
    		{
    			button_label     = "I have an inner class listener";
    			action_listener  = new InnerListener();
    			foreground_color = Color.GREEN;    		
    		}    			
    		else if (((String) combobox.getSelectedItem()).equals(LOCAL_LISTENER_NAME))
    		{
    			button_label     = "I have a local class listener";
        			
    			class LocalListener implements ActionListener
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    		    		JOptionPane.showMessageDialog(window, "Local listener called");
    		    		removeButton((JButton)e.getSource());
    				}
    			}
    		
    			action_listener  = new LocalListener();
    			foreground_color = Color.YELLOW;    		
    		}
    		else if (((String) combobox.getSelectedItem()).equals(ANONYMOUS_LISTENER_NAME))
    		{
    			button_label     = "I have an anonymous class listener";
    			action_listener  = new ActionListener() { 
    				public void actionPerformed(ActionEvent e)
    				{
    		    		JOptionPane.showMessageDialog(window, "Anonymous listener called");
    		    		removeButton((JButton)e.getSource());
    				}
    			};
    			foreground_color = Color.LIGHT_GRAY;    		
    		}
    		
    		JButton new_button = new JButton(button_label);
            window.getContentPane().add(new_button);      

    		new_button.setForeground(foreground_color);
            new_button.setBackground(background_color);
            new_button.addActionListener(action_listener);

            window.getContentPane().validate();      
    	}		   
    }
    
    public static void main(String[] argumentos) {
    	NestedClassButtons nestedclassbuttons = new NestedClassButtons();
    	nestedclassbuttons.execute();
    }

	public void removeButton(JButton source) {
		window.getContentPane().remove(source);
		window.getContentPane().repaint();
		window.getContentPane().validate();
	}
}






